package com.hm.ipay.utils;

import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.SMS;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cn.jpush.api.push.model.notification.PlatformNotification.ALERT;

public class JpushUtils {
    private static final String APP_KEY = "4d23f69c24fbc5dc5767cac8";
    private static final String MASTER_SECRET = "1c5245a13aeb0debc13686e6";
    private static final String TITLE = "ipay";

    public static PushResult sendJpushMsg(String msg, List<String> aliasList, String type, String platform, String orderId) {
        JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, ClientConfig.getInstance());
        Map<String, String> extras = new HashMap<String, String>();
        // 添加信息:msg 消息内容 type：1,订单推送 2，系统推送
        extras.put("type", type);
        extras.put("orderId", orderId + "");
        if (platform.equals("android")) {
            PushPayload payload = buildPushObject_android_alias_alert(msg, extras, aliasList);
            try {
                PushResult result = jpushClient.sendPush(payload);
                return result;
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (platform.equals("ios")) {
            PushPayload payload = buildPushObject_ios_alias_alert(msg, extras, aliasList);
            try {
                PushResult result = jpushClient.sendPush(payload);
                return result;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 透传android，由android自行设计弹出框格式
     */
    public static PushPayload buildPushObject_android_alias_alert(String msg, Map<String, String> extras, List<String> aliasList) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android())//必填 Platform类可设置推送的平台设备，android、ios、winphone或all；
                .setMessage(Message.newBuilder().setMsgContent(msg).addExtras(extras).build())////设置推送方式为透传
                .setAudience(Audience.alias(aliasList))//设置推送平台目标别名
                .build();
    }

    /**
     * 推送ios
     */
    public static PushPayload buildPushObject_ios_alias_alert(String msg, Map<String, String> extras, List<String> aliasList) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.ios())//必填 Platform类可设置推送的平台设备，android、ios、winphone；
                .setNotification(Notification.newBuilder()
                        .setAlert(msg)
                        .addPlatformNotification(IosNotification.newBuilder().addExtras(extras).build())
                        .build())//设置推送方式为推送
                .setAudience(Audience.alias(aliasList))//设置推送平台目标别名
                //.setOptions(Options.newBuilder().setApnsProduction(false).build())
                //通过setApnsProduction来制定ios推送环境True 表示推送生产环境，False 表示要推送开发环境； 如 果不指定则为推送生产环境
                .build();
    }


    public static PushResult sendMsg(String msg) {
        JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, ClientConfig.getInstance());

        // For push, all you need do is to build PushPayload object.
        PushPayload payload = buildPushObject_android_tag_alertWithTitle();

        try {
            PushResult result = jpushClient.sendPush(payload);

        } catch (APIConnectionException e) {
            // Connection error, should retry later

        } catch (APIRequestException e) {
            // Should review the error, and fix the request
        }
        return null;
    }

    public static PushPayload buildPushObject_android_tag_alertWithTitle() {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.all())
                .setNotification(Notification.android(ALERT, TITLE, null))
                .build();
    }


}


